//公共路径
const BASE_URL = "http://www.17youtui.com";
// const BASE_URL = "/api";

//日期格式   JS 时间格式CST转GMT
const getTaskTime = strDate => {
  // console.log("原始时间格式：" + strDate);
  if (null == strDate || "" == strDate) {
    return "";
  }
  var date = new Date(strDate);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = date.getDate();
  d = d < 10 ? "0" + d : d;
  var h = date.getHours();
  var minute = date.getMinutes();
  minute = minute < 10 ? "0" + minute : minute;
  var str = y + "-" + m + "-" + d;
  // console.log(str);
  return str;
};


// 作者：酸味的凯凯
// 链接：https://www.jianshu.com/p/36f4f456d1c7
// 來源：简书
// 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
//此处转载https://www.cnblogs.com/fj99/p/5502130.html
/**
 * 
 * @param {*base64字符串} dataurl
 * @return {Blob文件类型}  
 */
const dataURLtoBlob = (dataurl) => {
  var arr = dataurl.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {
    type: mime
  });
};
/**
 * 采用递归算法，在移动端也测试过，10M也基本没问题，现在的手机随便一张照片
 * 就上3M了，这下可以减少不少服务器的负担
 *
 * @param {*img标签的加载事件参数} img 
 * @return {*处理最终结果返回图片base64编码} base64data
 * 
 */

const scaleimg = (img) => {
  let height = img.currentTarget.height;
  let width = img.currentTarget.width;
  let base64data = '';
  let can = document.createElement("canvas");
  can.height = height;
  can.width = width;
  let casimg = can.getContext('2d');
  casimg.clearRect(0, 0, width, height);
  casimg.drawImage(img.currentTarget, 0, 0, width, height);
  base64data = can.toDataURL('image/jpeg'); //获取在canvas元素中的图片截图base64编码
  let size = Math.round(dataURLtoBlob(base64data).size / 1024); //获取压缩前的图片大小
  let maxsize = 800; //设置压缩后的最大值
  if (size > maxsize) {
    if (size < 1300) {
      img.currentTarget.height = Math.round(height * (3 / 4));
      img.currentTarget.width = Math.round(width * (3 / 4));
      return scaleimg(img)
    } else if (size < 1800) {
      img.currentTarget.height = Math.round(height * (2 / 3));
      img.currentTarget.width = Math.round(width * (2 / 3));
      return scaleimg(img)
    } else {
      img.currentTarget.height = Math.round(height / 2);
      img.currentTarget.width = Math.round(width / 2);
      return scaleimg(img)
    }
  } else {
    return base64data;
  }
}

module.exports = {
  BASE_URL,
  getTaskTime,
  dataURLtoBlob,
  scaleimg
};
